﻿using SF.DBUtility;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace SF.DAL
{
    /// <summary>
    /// 规则配置DAL
    /// </summary>
    public partial class bs_rule_config
    {
        public bs_rule_config()
        {

        }

        /// <summary>
        /// 是否存在（通过Num来查询）
        /// </summary>
        /// <param name="busnieseNum"></param>
        /// <param name="appNum"></param>
        /// <returns></returns>
        public bool ExistByNum(string busnieseNum, string appNum)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from bs_rule_config");
            strSql.Append(" where busnieseNum=@busnieseNum and appNum=@appNum");
            SqlParameter[] parameters ={
                                            new SqlParameter("@busnieseNum",SqlDbType.NVarChar),
                                            new SqlParameter("@appNum",SqlDbType.NVarChar)
                                        };
            parameters[0].Value = busnieseNum;
            parameters[1].Value = appNum;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public long Add(SF.Model.bs_rule_config model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into bs_rule_config(");
            strSql.Append("busnieseNum, appNum, is_distribution, recommendFollowExp, followExp, is_follow_Buy, twoPieceDiscount, upTwoPieceDiscount, freePostMoney, minWithdrawMoney, autoConfirmDay, level0BuyExp, level1BuyExp, level2BuyExp, level3BuyExp, level0ReturnScale, level1ReturnScale, level2ReturnScale, level3ReturnScale, shoppingCurrency, actionCurrency, publicFund)");
            strSql.Append(" values(");
            strSql.Append("@busnieseNum, @appNum, @is_distribution, @recommendFollowExp, @followExp, @is_follow_Buy, @twoPieceDiscount, @upTwoPieceDiscount, @freePostMoney, @minWithdrawMoney, @autoConfirmDay, @level0BuyExp, @level1BuyExp, @level2BuyExp, @level3BuyExp, @level0ReturnScale, @level1ReturnScale, @level2ReturnScale, @level3ReturnScale, @shoppingCurrency, @actionCurrency, @publicFund)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50),
					new SqlParameter("@is_distribution", SqlDbType.TinyInt,1),
					new SqlParameter("@recommendFollowExp", SqlDbType.Int,4),
					new SqlParameter("@followExp", SqlDbType.Int,4),
					new SqlParameter("@is_follow_Buy", SqlDbType.TinyInt,1),
					new SqlParameter("@twoPieceDiscount", SqlDbType.Decimal,5),
					new SqlParameter("@upTwoPieceDiscount", SqlDbType.Decimal,5),
					new SqlParameter("@freePostMoney", SqlDbType.Decimal,5),
					new SqlParameter("@minWithdrawMoney", SqlDbType.Decimal,5),
					new SqlParameter("@autoConfirmDay", SqlDbType.Int,4),
					new SqlParameter("@level0BuyExp", SqlDbType.Decimal,5),
					new SqlParameter("@level1BuyExp", SqlDbType.Decimal,5),
					new SqlParameter("@level2BuyExp", SqlDbType.Decimal,5),
					new SqlParameter("@level3BuyExp", SqlDbType.Decimal,5),
					new SqlParameter("@level0ReturnScale", SqlDbType.Decimal,5),
					new SqlParameter("@level1ReturnScale", SqlDbType.Decimal,5),
					new SqlParameter("@level2ReturnScale", SqlDbType.Decimal,5),
					new SqlParameter("@level3ReturnScale", SqlDbType.Decimal,5),
					new SqlParameter("@shoppingCurrency", SqlDbType.Decimal,5),
					new SqlParameter("@actionCurrency", SqlDbType.Decimal,5),
					new SqlParameter("@publicFund", SqlDbType.Decimal,5)};
            parameters[0].Value = model.busnieseNum;
            parameters[1].Value = model.appNum;
            parameters[2].Value = model.is_distribution;
            parameters[3].Value = model.recommendFollowExp;
            parameters[4].Value = model.followExp;
            parameters[5].Value = model.is_follow_Buy;
            parameters[6].Value = model.twoPieceDiscount;
            parameters[7].Value = model.upTwoPieceDiscount;
            parameters[8].Value = model.freePostMoney;
            parameters[9].Value = model.minWithdrawMoney;
            parameters[10].Value = model.autoConfirmDay;
            parameters[11].Value = model.level0BuyExp;
            parameters[12].Value = model.level1BuyExp;
            parameters[13].Value = model.level2BuyExp;
            parameters[14].Value = model.level3BuyExp;
            parameters[15].Value = model.level0ReturnScale;
            parameters[16].Value = model.level1ReturnScale;
            parameters[17].Value = model.level2ReturnScale;
            parameters[18].Value = model.level3ReturnScale;
            parameters[19].Value = model.shoppingCurrency;
            parameters[20].Value = model.actionCurrency;
            parameters[21].Value = model.publicFund;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt64(obj);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(SF.Model.bs_rule_config model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update bs_rule_config set ");
            strSql.Append("is_distribution=@is_distribution,");
            strSql.Append("recommendFollowExp=@recommendFollowExp,");
            strSql.Append("followExp=@followExp,");
            strSql.Append("is_follow_Buy=@is_follow_Buy,");
            strSql.Append("twoPieceDiscount=@twoPieceDiscount,");
            strSql.Append("upTwoPieceDiscount=@upTwoPieceDiscount,");
            strSql.Append("freePostMoney=@freePostMoney,");
            strSql.Append("minWithdrawMoney=@minWithdrawMoney,");
            strSql.Append("autoConfirmDay=@autoConfirmDay,");
            strSql.Append("level0BuyExp=@level0BuyExp,");
            strSql.Append("level1BuyExp=@level1BuyExp,");
            strSql.Append("level2BuyExp=@level2BuyExp,");
            strSql.Append("level3BuyExp=@level3BuyExp,");
            strSql.Append("level0ReturnScale=@level0ReturnScale,");
            strSql.Append("level1ReturnScale=@level1ReturnScale,");
            strSql.Append("level2ReturnScale=@level2ReturnScale,");
            strSql.Append("level3ReturnScale=@level3ReturnScale,");
            strSql.Append("shoppingCurrency=@shoppingCurrency,");
            strSql.Append("actionCurrency=@actionCurrency,");
            strSql.Append("publicFund=@publicFund");
            strSql.Append(" where busnieseNum=@busnieseNum");
            strSql.Append(" and appNum=@appNum");
            SqlParameter[] parameters = {
					new SqlParameter("@is_distribution", SqlDbType.TinyInt,1),
					new SqlParameter("@recommendFollowExp", SqlDbType.Int,4),
					new SqlParameter("@followExp", SqlDbType.Int,4),
					new SqlParameter("@is_follow_Buy", SqlDbType.TinyInt,1),
					new SqlParameter("@twoPieceDiscount", SqlDbType.Decimal,5),
					new SqlParameter("@upTwoPieceDiscount", SqlDbType.Decimal,5),
					new SqlParameter("@freePostMoney", SqlDbType.Decimal,5),
					new SqlParameter("@minWithdrawMoney", SqlDbType.Decimal,5),
					new SqlParameter("@autoConfirmDay", SqlDbType.Int,4),
					new SqlParameter("@level0BuyExp", SqlDbType.Decimal,5),
					new SqlParameter("@level1BuyExp", SqlDbType.Decimal,5),
					new SqlParameter("@level2BuyExp", SqlDbType.Decimal,5),
					new SqlParameter("@level3BuyExp", SqlDbType.Decimal,5),
					new SqlParameter("@level0ReturnScale", SqlDbType.Decimal,5),
					new SqlParameter("@level1ReturnScale", SqlDbType.Decimal,5),
					new SqlParameter("@level2ReturnScale", SqlDbType.Decimal,5),
					new SqlParameter("@level3ReturnScale", SqlDbType.Decimal,5),
					new SqlParameter("@shoppingCurrency", SqlDbType.Decimal,5),
					new SqlParameter("@actionCurrency", SqlDbType.Decimal,5),
					new SqlParameter("@publicFund", SqlDbType.Decimal,5),
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50)};
            parameters[0].Value = model.is_distribution;
            parameters[1].Value = model.recommendFollowExp;
            parameters[2].Value = model.followExp;
            parameters[3].Value = model.is_follow_Buy;
            parameters[4].Value = model.twoPieceDiscount;
            parameters[5].Value = model.upTwoPieceDiscount;
            parameters[6].Value = model.freePostMoney;
            parameters[7].Value = model.minWithdrawMoney;
            parameters[8].Value = model.autoConfirmDay;
            parameters[9].Value = model.level0BuyExp;
            parameters[10].Value = model.level1BuyExp;
            parameters[11].Value = model.level2BuyExp;
            parameters[12].Value = model.level3BuyExp;
            parameters[13].Value = model.level0ReturnScale;
            parameters[14].Value = model.level1ReturnScale;
            parameters[15].Value = model.level2ReturnScale;
            parameters[16].Value = model.level3ReturnScale;
            parameters[17].Value = model.shoppingCurrency;
            parameters[18].Value = model.actionCurrency;
            parameters[19].Value = model.publicFund;
            parameters[20].Value = model.busnieseNum;
            parameters[21].Value = model.appNum;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Edit(SF.Model.bs_rule_config model)
        {
            var isExist = ExistByNum(model.busnieseNum, model.appNum);
            if (!isExist)
            {
                if (Add(model) > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return Update(model);
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(long id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from bs_rule_config ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="busnieseNum"></param>
        /// <param name="appNum"></param>
        /// <returns></returns>
        public SF.Model.bs_rule_config GetModel(string busnieseNum, string appNum)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 id,busnieseNum, appNum, is_distribution, recommendFollowExp, followExp, is_follow_Buy, twoPieceDiscount, upTwoPieceDiscount, freePostMoney, minWithdrawMoney, autoConfirmDay, level0BuyExp, level1BuyExp, level2BuyExp, level3BuyExp, level0ReturnScale, level1ReturnScale, level2ReturnScale, level3ReturnScale, shoppingCurrency, actionCurrency, publicFund from bs_rule_config ");
            strSql.Append(" where busnieseNum=@busnieseNum");
            strSql.Append(" and appNum=@appNum");
            SqlParameter[] parameters = {
					new SqlParameter("@busnieseNum", SqlDbType.NVarChar,50),
					new SqlParameter("@appNum", SqlDbType.NVarChar,50)
			};
            parameters[0].Value = busnieseNum;
            parameters[1].Value = appNum;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SF.Model.bs_rule_config DataRowToModel(DataRow row)
        {
            SF.Model.bs_rule_config model = new SF.Model.bs_rule_config();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = long.Parse(row["id"].ToString());
                }
                if (row["busnieseNum"] != null)
                {
                    model.busnieseNum = row["busnieseNum"].ToString();
                }
                if (row["appNum"] != null)
                {
                    model.appNum = row["appNum"].ToString();
                }
                if (row["is_distribution"] != null && row["is_distribution"].ToString() != "")
                {
                    model.is_distribution = int.Parse(row["is_distribution"].ToString());
                }
                if (row["recommendFollowExp"] != null && row["recommendFollowExp"].ToString() != "")
                {
                    model.recommendFollowExp = int.Parse(row["recommendFollowExp"].ToString());
                }
                if (row["followExp"] != null && row["followExp"].ToString() != "")
                {
                    model.followExp = int.Parse(row["followExp"].ToString());
                }
                if (row["is_follow_Buy"] != null && row["is_follow_Buy"].ToString() != "")
                {
                    model.is_follow_Buy = int.Parse(row["is_follow_Buy"].ToString());
                }
                if (row["twoPieceDiscount"] != null && row["twoPieceDiscount"].ToString() != "")
                {
                    model.twoPieceDiscount = decimal.Parse(row["twoPieceDiscount"].ToString());
                }
                if (row["upTwoPieceDiscount"] != null && row["upTwoPieceDiscount"].ToString() != "")
                {
                    model.upTwoPieceDiscount = decimal.Parse(row["upTwoPieceDiscount"].ToString());
                }
                if (row["minWithdrawMoney"] != null && row["minWithdrawMoney"].ToString() != "")
                {
                    model.minWithdrawMoney = decimal.Parse(row["minWithdrawMoney"].ToString());
                }
                if (row["autoConfirmDay"] != null && row["autoConfirmDay"].ToString() != "")
                {
                    model.autoConfirmDay = int.Parse(row["autoConfirmDay"].ToString());
                }
                if (row["level0BuyExp"] != null && row["level0BuyExp"].ToString() != "")
                {
                    model.level0BuyExp = decimal.Parse(row["level0BuyExp"].ToString());
                }
                if (row["level1BuyExp"] != null && row["level1BuyExp"].ToString() != "")
                {
                    model.level1BuyExp = decimal.Parse(row["level1BuyExp"].ToString());
                }
                if (row["level2BuyExp"] != null && row["level2BuyExp"].ToString() != "")
                {
                    model.level2BuyExp = decimal.Parse(row["level2BuyExp"].ToString());
                }
                if (row["level3BuyExp"] != null && row["level3BuyExp"].ToString() != "")
                {
                    model.level3BuyExp = decimal.Parse(row["level3BuyExp"].ToString());
                }
                if (row["level0ReturnScale"] != null && row["level0ReturnScale"].ToString() != "")
                {
                    model.level0ReturnScale = decimal.Parse(row["level0ReturnScale"].ToString());
                }
                if (row["level1ReturnScale"] != null && row["level1ReturnScale"].ToString() != "")
                {
                    model.level1ReturnScale = decimal.Parse(row["level1ReturnScale"].ToString());
                }
                if (row["level2ReturnScale"] != null && row["level2ReturnScale"].ToString() != "")
                {
                    model.level2ReturnScale = decimal.Parse(row["level2ReturnScale"].ToString());
                }
                if (row["level3ReturnScale"] != null && row["level3ReturnScale"].ToString() != "")
                {
                    model.level3ReturnScale = decimal.Parse(row["level3ReturnScale"].ToString());
                }
                //
                if (row["shoppingCurrency"] != null && row["shoppingCurrency"].ToString() != "")
                {
                    model.shoppingCurrency = decimal.Parse(row["shoppingCurrency"].ToString());
                }
                if (row["actionCurrency"] != null && row["actionCurrency"].ToString() != "")
                {
                    model.actionCurrency = decimal.Parse(row["actionCurrency"].ToString());
                }
                if (row["publicFund"] != null && row["publicFund"].ToString() != "")
                {
                    model.publicFund = decimal.Parse(row["publicFund"].ToString());
                }
            }
            return model;
        }
    }
}
